//
//  SEQRView.m
//  ShareElectricity
//
//  Created by sunsoft on 2017/6/10.
//  Copyright © 2017年 sunnsoft.HH. All rights reserved.
//

#import "SEQRView.h"


@implementation SEQRView{
    
    UIImageView *qrLine;
    CGFloat qrLineY;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    if (qrLine == nil) {
        NotificationCenterADD(self, @selector(show), startAmoution, nil);
        [self initQRLine];
//        [self show];
    }
}

- (void)initQRLine {
    
//    qrLine  = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - self.transparentArea.width / 2, self.bounds.size.height / 2 - self.transparentArea.height / 2, self.transparentArea.width, 1.5)];
//    qrLine.backgroundColor = BlueColor;
//    qrLine.contentMode = UIViewContentModeScaleToFill;
//    [self addSubview:qrLine];
//    qrLineY = qrLine.frame.origin.y;
    
//    qrLine  = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - self.transparentArea.width / 2, 0, self.transparentArea.width,self.transparentArea.width)];
//    qrLine.image = [UIImage imageNamed:@"scanView"];
//    qrLine.contentMode = UIViewContentModeScaleToFill;
//    [self addSubview:qrLine];
//    qrLineY = qrLine.frame.origin.y;
    
    qrLine = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2 - self.transparentArea.width / 2, self.bounds.size.height / 2 - self.transparentArea.height / 2, self.transparentArea.width, 0)];
    qrLine.image = [UIImage imageNamed:@"scanView"];
    [self addSubview:qrLine];
//    self.scanceImg = scanView;
    [self show];
}

- (void)show {
    
    qrLine.hidden = NO;
    [qrLine.layer removeAllAnimations];
    
    CGRect frame = CGRectMake(self.bounds.size.width / 2 - self.transparentArea.width / 2, self.bounds.size.height / 2 - self.transparentArea.height / 2, self.transparentArea.width, 0);
    frame.size.height = 0;
    qrLine.frame = frame;
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat animations:^{
        CGRect frame = qrLine.frame;
        frame.size.height = self.transparentArea.width;
        qrLine.frame = frame;
    } completion:nil];
}

- (void)stop {
    
    [qrLine.layer removeAllAnimations];
//    [qrLine removeFromSuperview];
    qrLine.hidden = YES;
}

- (void)drawRect:(CGRect)rect {
    
    //整个二维码扫描界面的颜色
    CGSize screenSize =[UIScreen mainScreen].bounds.size;
    CGRect screenDrawRect =CGRectMake(0, 0, screenSize.width,screenSize.height);
    
    //中间清空的矩形框
    CGRect clearDrawRect = CGRectMake(screenDrawRect.size.width / 2 - self.transparentArea.width / 2,
                                      screenDrawRect.size.height / 2 - self.transparentArea.height / 2,
                                      self.transparentArea.width,self.transparentArea.height);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self addScreenFillRect:ctx rect:screenDrawRect];
    
    [self addCenterClearRect:ctx rect:clearDrawRect];
    
    [self addWhiteRect:ctx rect:clearDrawRect];
    
    [self addCornerLineWithContext:ctx rect:clearDrawRect];
    
    
}

- (void)addScreenFillRect:(CGContextRef)ctx rect:(CGRect)rect {
    
    CGContextSetRGBFillColor(ctx, 40 / 255.0,40 / 255.0,40 / 255.0,0.5);
    CGContextFillRect(ctx, rect);   //draw the transparent layer
}

- (void)addCenterClearRect :(CGContextRef)ctx rect:(CGRect)rect {
    
    CGContextClearRect(ctx, rect);  //clear the center rect  of the layer
}

- (void)addWhiteRect:(CGContextRef)ctx rect:(CGRect)rect {
    
    CGContextStrokeRect(ctx, rect);
    CGContextSetRGBStrokeColor(ctx, 75 / 255.0, 151 / 255.0, 253 / 255.0, 1);
    CGContextSetLineWidth(ctx, 0.8);
    CGContextAddRect(ctx, rect);
    CGContextStrokePath(ctx);
}

- (void)addCornerLineWithContext:(CGContextRef)ctx rect:(CGRect)rect{
    
    CGFloat cornerW = 4;
    CGFloat cornerL = 15;
    CGFloat rectW = 0.8;
    
    //画四个边角
    CGContextSetLineWidth(ctx, cornerW);

    // 上边角颜色
    CGContextSetRGBStrokeColor(ctx, 75 / 255.0, 151 / 255.0, 253 / 255.0, 1);
    
    //左上角
    CGPoint leftTopPoint = CGPointMake(rect.origin.x - rectW * 0.5, rect.origin.y - rectW * 0.5);
    CGPoint poinsTopLeftA[] = {
        CGPointMake(leftTopPoint.x - cornerW * 0.5, leftTopPoint.y - cornerW),
        CGPointMake(leftTopPoint.x - cornerW * 0.5, leftTopPoint.y - cornerW + cornerL)
    };
    
    CGPoint poinsTopLeftB[] = {CGPointMake(leftTopPoint.x - cornerW, leftTopPoint.y - cornerW * 0.5),CGPointMake(leftTopPoint.x - cornerW + cornerL, leftTopPoint.y - cornerW * 0.5)};
    [self addLine:poinsTopLeftA pointB:poinsTopLeftB ctx:ctx];
    
    //右上角
    CGPoint rightTopPoint = CGPointMake(rect.origin.x + rect.size.height + rectW * 0.5, rect.origin.y - rectW * 0.5);
    CGPoint poinsTopRightA[] = {CGPointMake(rightTopPoint.x + cornerW - cornerL, rightTopPoint.y - cornerW * 0.5),CGPointMake(rightTopPoint.x + cornerW, rightTopPoint.y - cornerW * 0.5)};
    CGPoint poinsTopRightB[] = {CGPointMake(rightTopPoint.x + cornerW * 0.5, rightTopPoint.y - cornerW), CGPointMake(rightTopPoint.x + cornerW * 0.5, rightTopPoint.y - cornerW + cornerL)};
    [self addLine:poinsTopRightA pointB:poinsTopRightB ctx:ctx];
    
    CGContextStrokePath(ctx);
    
    // 下边角颜色
    CGContextSetRGBStrokeColor(ctx, 75 / 255.0, 151 / 255.0, 253 / 255.0, 1);
    
    //左下角
    CGPoint leftBottomPoint = CGPointMake(rect.origin.x - rectW * 0.5, rect.origin.y + rect.size.height + rectW * 0.5);
    CGPoint poinsBottomLeftA[] = {CGPointMake(leftBottomPoint.x - cornerW * 0.5, leftBottomPoint.y + cornerW - cornerL), CGPointMake(leftBottomPoint.x - cornerW * 0.5, leftBottomPoint.y + cornerW)};
    CGPoint poinsBottomLeftB[] = {CGPointMake(leftBottomPoint.x - cornerW, leftBottomPoint.y + cornerW * 0.5) ,CGPointMake(leftBottomPoint.x - cornerW + cornerL, leftBottomPoint.y + cornerW * 0.5)};
    [self addLine:poinsBottomLeftA pointB:poinsBottomLeftB ctx:ctx];
    
    // 右下角
    CGPoint rightBottomPoint = CGPointMake(rect.origin.x + rect.size.height + rectW * 0.5, rect.origin.y + rect.size.height + rectW * 0.5);
    CGPoint poinsBottomRightA[] = {CGPointMake(rightBottomPoint.x + cornerW * 0.5, rightBottomPoint.y + cornerW - cornerL),CGPointMake(rightBottomPoint.x + cornerW * 0.5,rightBottomPoint.y + cornerW)};
    CGPoint poinsBottomRightB[] = {CGPointMake(rightBottomPoint.x + cornerW - cornerL, rightBottomPoint.y + cornerW * 0.5),CGPointMake(rightBottomPoint.x + cornerW, rightBottomPoint.y + cornerW * 0.5)};
    [self addLine:poinsBottomRightA pointB:poinsBottomRightB ctx:ctx];
    CGContextStrokePath(ctx);
}

- (void)addLine:(CGPoint[])pointA pointB:(CGPoint[])pointB ctx:(CGContextRef)ctx {
    CGContextAddLines(ctx, pointA, 2);
    CGContextAddLines(ctx, pointB, 2);
}
@end
